﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Longbow.Caching;
using System.Text;

namespace HuaCheng.DataAccess
{
    /// <summary>
    /// 设备日志的数据库操作
    /// </summary>
    public class DeviceLogHelper
    {
        internal const string RetrieveDeviceLogDataKey = "DeviceLogHelper-RetrieveDeviceLog";
        /// <summary>
        /// log_mid表新增记录
        /// </summary>
        public static void InsertDeviceLog(DeviceLog deviceLog)
        {
            if (deviceLog == null) throw new ArgumentNullException("deviceLog");
            string sql = "insert into log_mid(dev_ip,cabinet_id,mid_id,chl_id,test_id,operation_type,test_atime,operator) values (@dev_ip,@gid,@mid,@chl_id,@test_id,@operation_type,@test_atime,@operator)";
            try
            {
                using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
                {
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@dev_ip", deviceLog.DevIp));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", deviceLog.GID));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", deviceLog.MID));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chl_id", deviceLog.ChlId));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@test_id", deviceLog.TestId));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@operation_type", deviceLog.OperationType));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@test_atime", deviceLog.TestATime));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@operator", deviceLog.Operator));
                    DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
                }
                CacheManager.Clear(k => k == RetrieveDeviceLogDataKey);
            }
            catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
        }
        /// <summary>
        /// 查询log_mid表所有信息
        /// </summary>
        /// <returns></returns>
        public static List<DeviceLog> RetrieveDeviceLog(string dev_ip,int gid,int mid, int chl_id)        
        {
            string key = RetrieveDeviceLogDataKey;
            return CacheManager.GetOrAdd(key, k =>
                {
                    List<DeviceLog> ret = new List<DeviceLog>();
                    try
                    {
                        string sql = "select * from log_mid where dev_ip=@dev_ip and cabinet_id=@gid and mid_id=@mid and chl_id=@chl_id";
                        DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@dev_ip", dev_ip));
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chl_id", chl_id));
                        using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    DeviceLog deviceLog = new DeviceLog()
                                    {
                                        DevIp = Convert.ToString(reader["dev_ip"]),
                                        GID = Convert.ToInt32(reader["cabinet_id"]),
                                        MID = Convert.ToInt32(reader["mid_id"]),
                                        ChlId = Convert.ToInt32(reader["chl_id"]),
                                        TestId = Convert.ToString(reader["test_id"]),
                                        OperationType = Convert.ToString(reader["operation_type"]),
                                        TestATime = Convert.ToDateTime(reader["test_atime"]),
                                        Operator=Convert.ToString(reader["operator"])
                                    };
                                    ret.Add(deviceLog);
                                }
                            }
                        }
                    }
                    catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
                    return ret;
                }, RetrieveDeviceLogDataKey);
        }
        /// <summary>
        /// 按测试id查询log_mid表信息
        /// </summary>
        /// <returns></returns>
        public static List<string> RetrieveDeviceLogByTestId(string test_id,string dev_ip, int gid, int mid, int chl_id)
        {
            #region 原来代码
            //string key = RetrieveDeviceLogDataKey;
            //return CacheManager.GetOrAdd(key, k =>
            //{
            //    List<DeviceLog> ret = new List<DeviceLog>();
            //    try
            //    {
            //        string sql = "select * from log_mid where test_id=@test_id and dev_ip=@dev_ip and cabinet_id=@gid and mid_id=@mid and chl_id=@chl_id";
            //        DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
            //        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@test_id", test_id));
            //        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@dev_ip", dev_ip));
            //        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
            //        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
            //        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chl_id", chl_id));
            //        using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
            //        {
            //            if (reader.HasRows)
            //            {
            //                while (reader.Read())
            //                {
            //                    DeviceLog deviceLog = new DeviceLog()
            //                    {
            //                        DevIp = Convert.ToString(reader["dev_ip"]),
            //                        GID = Convert.ToInt32(reader["cabinet_id"]),
            //                        MID = Convert.ToInt32(reader["mid_id"]),
            //                        ChlId = Convert.ToInt32(reader["chl_id"]),
            //                        TestId = Convert.ToString(reader["test_id"]),
            //                        OperationType = Convert.ToString(reader["operation_type"]),
            //                        TestATime = Convert.ToDateTime(reader["test_atime"]),
            //                        Operator = Convert.ToString(reader["operator"])
            //                    };
            //                    ret.Add(deviceLog);
            //                }
            //            }
            //        }
            //    }
            //    catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
            //    return ret;
            //}, RetrieveDeviceLogDataKey);
            #endregion
            string key = RetrieveDeviceLogDataKey;
            return CacheManager.GetOrAdd(key, k =>
            {
                List<string> ret = new List<string>();
                try
                {
                    string sql = "select * from log_mid where test_id=@test_id and dev_ip=@dev_ip and cabinet_id=@gid and mid_id=@mid and chl_id=@chl_id";
                    DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@test_id", test_id));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@dev_ip", dev_ip));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chl_id", chl_id));
                    using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                    {
                        if (reader.HasRows)
                        {
                            StringBuilder sb = new StringBuilder();
                            while (reader.Read())
                            {
                                sb.Append("ChlId=" + Convert.ToInt32(reader["chl_id"]) + "/");
                                sb.Append("DevIp=" + Convert.ToString(reader["dev_ip"]) + "/");
                                sb.Append("GID=" + Convert.ToInt32(reader["cabinet_id"]) + "/");
                                sb.Append("MID=" + Convert.ToInt32(reader["mid_id"]) + "/");
                                sb.Append("OperationType=" + Convert.ToString(reader["operation_type"]) + "/");
                                sb.Append("Operator=" + Convert.ToString(reader["operator"]) + "/");
                                sb.Append("TestId=" + Convert.ToString(reader["test_id"]) + "/");
                                sb.Append("TestATime=" + Convert.ToDateTime(reader["test_atime"]) + "-");
                                ret.Add(sb.ToString());
                                sb.Clear();
                            }
                        }
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
                return ret;
            }, RetrieveDeviceLogDataKey);
        }
    }
}
